package com.lvyuely.common.web.core;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Simple to Introduction
 * <pre>
 * <b>项目</b> :  lvyuely-archetype-webapp
 * <b>包名</b> :  com.lvyuely.common.web.core
 * <b>类名</b> :  AbstractSession
 * <b>描述</b> :
 * <b>版权</b> :  Copyright (c) 1999-2015 广联达软件股份有限公司
 * </pre>
 *
 * @author :   lvyue
 * @version :  1.0
 * @date :     15/1/7 23:09
 */
public abstract class AbstractRequest {

    private static final Logger logger = Logger.getLogger(AbstractRequest.class);

    /**
     * 从cookie获得指定数据
     *
     * @param request
     * @param key
     * @return
     */
    public String get(HttpServletRequest request, String key) {
        try {
            if (StringUtils.isBlank(key)) {
                return null;
            }
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie ck : cookies) {
                    if (key.equals(ck.getName())) {
                        return ck.getValue();
                    }
                }
            }
        } catch (Exception e) {
            logger.error("", e);
        }
        return null;
    }

    /**
     * 移除 cookie
     *
     * @param response 用户请求响应对象
     * @param name     cookie key
     * @return
     */
    public boolean delete(HttpServletResponse response, String name) {
        try {
            if (StringUtils.isBlank(name)) {
                return false;
            }
            Cookie _c = new Cookie(name, "");
            _c.setPath("/");
            _c.setMaxAge(0);
            response.addCookie(_c);
            return true
                    ;
        } catch (Exception e) {
            logger.error("", e);
        }
        return false;
    }

    /**
     * 查询用户请求IP
     *
     * @return 用户IP地址
     */
    public String ip(HttpServletRequest request) {
        String user_access_ip = null;
        try {
            // 获取IP地址
            if (StringUtils.isBlank(request.getHeader("x-forwarded-for"))) {
                user_access_ip = request.getRemoteAddr();
            } else {
                user_access_ip = request.getHeader("x-forwarded-for");
            }
        } catch (Exception e) {
            logger.error("", e);
        }
        return user_access_ip;
    }

    /**
     * 设置cookie
     *
     * @param key
     * @param value
     * @param path
     */
    public void cookie(HttpServletResponse response, String key, String value, String path) {
        // 创建cookie
        Cookie _c = new Cookie(key, value);
        // 设置domain
        // _c.setDomain(Constants.BASE_DOMAIN);
        // 设置路径
        _c.setPath(path);
        // 设置cookie
        response.addCookie(_c);
    }

    /**
     * 设置cookie
     *
     * @param key
     * @param value
     */
    public void cookie(HttpServletResponse response, String key, String value) {
        // 创建cookie
        Cookie _c = new Cookie(key, value);
        // 设置cookie
        response.addCookie(_c);
    }


    /**
     * @param key
     * @param value
     * @param path
     * @param expression
     */
    public void cookie(HttpServletResponse response, String key, String value, String path, int expression) {
        // 创建cookie
        Cookie _c = new Cookie(key, value);
        // 设置路径
        _c.setPath(path);
        // 设置cookie时间
        _c.setMaxAge(expression);
        // 设置cookie
        response.addCookie(_c);
    }

    /**
     * @param key
     * @param value
     * @param expression
     */
    public void cookie(HttpServletResponse response, String key, String value, int expression) {
        // 创建cookie
        Cookie _c = new Cookie(key, value);
        // 设置cookie时间
        _c.setMaxAge(expression);
        // 设置cookie
        response.addCookie(_c);
    }

    /**
     * @param key
     * @param value
     * @param domain
     * @param path
     */
    public void cookie(HttpServletResponse response, String key, String value, String domain, String path) {
        // 创建cookie
        Cookie _c = new Cookie(key, value);
        // 设置
        _c.setDomain(domain);
        // 设置路径
        _c.setPath(path);
        // 设置cookie
        response.addCookie(_c);

    }

    /**
     * 设置cookie
     *
     * @param key
     * @param value
     * @param domain
     * @param path
     * @param expression
     */
    public void cookie(HttpServletResponse response, String key, String value, String domain, String path, int expression) {
        // 创建cookie
        Cookie _c = new Cookie(key, value);
        // 设置
        _c.setDomain(domain);
        // 设置路径
        _c.setPath(path);
        // 设置cookie时间
        _c.setMaxAge(expression);
        // 设置cookie
        response.addCookie(_c);
    }
}
